package com.minggo.charmword.logic;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;

import com.minggo.charmword.model.Result;
import com.minggo.charmword.net.ApiUrl;
import com.minggo.charmword.net.ApiUtil;
import com.minggo.charmword.service.CharmWordApplication;
import com.minggo.charmword.util.AppContext;

/**
 * 上传头像
 * 
 * @author liangchunrong
 * @time 2014-7-25 上午11:17:45
 */
public class UpdateAvtarUtil extends AsyncTask<Void, Void, Result> {

	public static final int AVTAR_SUCCESS = 10004;
	private Handler handler;
	private String userid;
	private File file;

	public UpdateAvtarUtil(Context context, Handler handler, String userid, File file) {
		this.handler = handler;
		this.file = file;
		this.userid = userid;
	}

	/**
	 * 上传头像
	 * @return
	 */
	public Result updateAvtar() {
		if (!AppContext.isNetworkConnected(CharmWordApplication.getInstance())) {
			return null;
		}
		Result result = null;
		try {
			Map<String, Object> params = ApiUtil.addRequiredParamByPost();
			params.put("userid", userid);
			Map<String, File> files = new HashMap<String, File>();
			files.put("file", file);
			
			result = ApiUtil.getResultByPost(ApiUrl.USER_UPLOAD_AVATAR, params, files, false, "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		

		return result;
	}

	@Override
	protected Result doInBackground(Void... params) {
		Result result = updateAvtar();
		return result;
	}

	@Override
	protected void onPostExecute(Result result) {
		super.onPostExecute(result);
		handler.obtainMessage(AVTAR_SUCCESS, result).sendToTarget();
	}

}
